package com.hjq.base.rollpager;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hjq.base.R;
import com.hjq.base.rollpager.rollpageradapter.LoopPagerAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

public class CommonAdapter extends LoopPagerAdapter {
    private String[] imageUrls; //自定义轮播图的资源 ImageLoader载入方式，网络Url或者"file://"+图片文件的本地路径

    private DisplayImageOptions options; //DisplayImageOptions是用于设置图片显示的类

    public CommonAdapter(Context context, String[] imageUrls, RollPagerView viewPager, int Corner) {
        super(viewPager);
        this.imageUrls = imageUrls;
        initImageLoader(Corner);
    }

    public void setImageUrls(String[] imageUrls) {
        this.imageUrls = imageUrls;
        notifyDataSetChanged();
    }

    @Override
    public View getView(ViewGroup container, int position) {
        ImageView imageView = new ImageView(container.getContext());
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ImageLoader.getInstance().displayImage(imageUrls[position], imageView, options);
        return imageView;
    }

    @Override
    public int getRealCount() {
        return imageUrls.length;
    }

    public void initImageLoader(int Corner) {
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_image_downloading)            // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.ic_image_downloadfailed)    // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.ic_image_downloadfailed)        // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                            // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(Corner))	// 设置成圆角图片
                .build();                                    // 创建配置过得DisplayImageOption对象

    }
}
